package com.uchengwang.ui.cm_ui.cycle.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnItemLongClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;
import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.listener.OnHttpResultListener;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.cycle.adapter.TalentListAdapter;
import com.uchengwang.ui.cm_ui.cycle.bean.TalentPeopleBean;
import com.uchengwang.ui.cm_ui.cycle.http.CycleHttpUtils;

import java.util.List;

/**
 * Created by U城网 on 2018/6/15.
 * 达人榜
 */

public class TalentListActivity extends BaseWithTitleActivity implements View.OnClickListener{

    private LRecyclerView rv;
    private TalentListAdapter adapter;
    private TextView name1, school1, num1, school3, name3, num3, num2, school2, name2;
    private ImageView list_bg, head1, head2, head3;
    private long cycleItemId;  //主题id

    private LRecyclerViewAdapter mLRecyclerViewAdapter;
    private int page, totalPage;

    private TalentPeopleBean  bean1, bean2, bean3;
    private LinearLayout ln_3, ln_2, ln_1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_talent_list);
        setTitle("达人榜");

        cycleItemId = getIntent().getLongExtra(BaseConfig.CYCLE_ITEM_ID, 0);

        initView();
    }

    private void initView(){
        name1 = (TextView) findViewById(R.id.name1);
        name1.requestFocus();
        name1.setFocusableInTouchMode(true);

        ln_1 = (LinearLayout) findViewById(R.id.ln_1);
        ln_1.setOnClickListener(this);
        ln_2 = (LinearLayout) findViewById(R.id.ln_2);
        ln_2.setOnClickListener(this);
        ln_3 = (LinearLayout) findViewById(R.id.ln_3);
        ln_3.setOnClickListener(this);

        name1 = (TextView) findViewById(R.id.name1);
        head1 = (ImageView) findViewById(R.id.head1);
        school1 = (TextView) findViewById(R.id.school1);
        num1 = (TextView) findViewById(R.id.num1);

        name2 = (TextView) findViewById(R.id.name2);
        head2 = (ImageView) findViewById(R.id.head2);
        school2 = (TextView) findViewById(R.id.school2);
        num2 = (TextView) findViewById(R.id.num2);


        name3 = (TextView) findViewById(R.id.name3);
        head3 = (ImageView) findViewById(R.id.head3);
        school3 = (TextView) findViewById(R.id.school3);
        num3 = (TextView) findViewById(R.id.num3);


        rv = (LRecyclerView) findViewById(R.id.talent_list_rv);
        list_bg = (ImageView) findViewById(R.id.talent_list_bg);

        initData();
    }

    private void initData(){
        ImageManager.getInstance().setRoundSize(2).loadImage(this, list_bg, R.drawable.daren_bg, ImageManager.ImageType.ROUND);

        adapter = new TalentListAdapter(this);
        rv.setLayoutManager(new LinearLayoutManager(this));
        mLRecyclerViewAdapter = new LRecyclerViewAdapter(adapter);
        rv.setAdapter(mLRecyclerViewAdapter);


        //设置头部加载颜色
        rv.setHeaderViewColor(R.color.comment_color, R.color.uni_title_bar ,R.color.white);
        //设置底部加载颜色
        rv.setFooterViewColor(R.color.comment_color, R.color.uni_title_bar ,R.color.white);
        //设置底部加载文字提示
        rv.setFooterViewHint("拼命加载中","已经全部为你呈现了","网络不给力啊，点击再试一次吧");

        rv.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getData(true);
            }
        });

        rv.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                page++;
                if (page <= totalPage){
                    //当前请求页大于总页数，不再请求
                    getData(false);
                } else {
                    rv.setNoMore(true);
                }
            }
        });

        mLRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(TalentListActivity.this, LoadCycleWebHtmlActivity.class);
                intent.putExtra(BaseConfig.IS_HIDE_RIGHT, true);
                intent.putExtra(BaseConfig.URL_TITLE, adapter.getData().get(position).getUid());
                intent.putExtra(BaseConfig.URL, BaseConfig.BaseWebURL + "/user/"+adapter.getData().get(position).getUid());
                startActivity(intent);
            }

        });

        mLRecyclerViewAdapter.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public void onItemLongClick(View view, int position) {
            }
        });

        rv.setRefreshProgressStyle(ProgressStyle.LineSpinFadeLoader);
        rv.setLoadingMoreProgressStyle(ProgressStyle.BallSpinFadeLoader);

        rv.forceToRefresh();

    }

    private void getData(final boolean isRefresh){
        CycleHttpUtils.getUtils().getTalentPeoples(this, cycleItemId, page, 10, null, null, 0,
                "talent_score", BaseConfig.DESC, new OnHttpResultListener<TalentPeopleBean>() {
                    @Override
                    public void onResult(int status, String msg, int total, List<TalentPeopleBean> list) {

                        if (isRefresh){
                            rv.refreshComplete(10);
                            mLRecyclerViewAdapter.notifyDataSetChanged();
                        }else {
                            rv.refreshComplete(10);// REQUEST_COUNT为每页加载数量
                        }

                        if (list != null){
                            totalPage = total;

                            if (isRefresh){
                                if (list.size() >0 && list.get(0) != null){
                                    bean1 = list.get(0);
                                    list.remove(0);
                                }

                                if (list.size() >0 && list.get(0) != null){
                                    bean2 = list.get(0);
                                    list.remove(0);
                                }

                                if (list.size() >0 && list.get(0) != null){
                                    bean3 = list.get(0);
                                    list.remove(0);
                                }


                                initNum();

                                adapter.setData(list);
                            }else {
                                adapter.getData().addAll(list);
                                adapter.notifyDataSetChanged();
                            }
                        }
                    }
        });
    }

    //重新加载排名
    private void initNum(){
        if (bean1 != null){
            ln_1.setVisibility(View.VISIBLE);
            ImageManager.getInstance().loadImage(this, head1, bean1.getUserAvatar(), ImageManager.ImageType.CIRCLE);
            name1.setText(bean1.getNickName());
            school1.setText(bean1.getUniName());
            num1.setText(bean1.getTalentScore() + "");
        }else {
            ln_1.setVisibility(View.GONE);
        }
        if (bean2 != null){
            ln_2.setVisibility(View.VISIBLE);
            ImageManager.getInstance().loadImage(this, head2, bean2.getUserAvatar(), ImageManager.ImageType.CIRCLE);
            name2.setText(bean2.getNickName());
            school2.setText(bean2.getUniName());
            num2.setText(bean2.getTalentScore() + "");
        }else {
            ln_2.setVisibility(View.GONE);
        }

        if (bean3 != null){
            ln_3.setVisibility(View.VISIBLE);
            ImageManager.getInstance().loadImage(this, head3, bean3.getUserAvatar(), ImageManager.ImageType.CIRCLE);
            name3.setText(bean3.getNickName());
            school3.setText(bean3.getUniName());
            num3.setText(bean3.getTalentScore() + "");
        }else {
            ln_3.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this, LoadCycleWebHtmlActivity.class);
        switch (v.getId()){
            case R.id.ln_1:
                intent.putExtra(BaseConfig.URL, BaseConfig.BaseWebURL + "/user/"+bean1.getUid());
                break;
            case R.id.ln_2:
                intent.putExtra(BaseConfig.URL, BaseConfig.BaseWebURL + "/user/"+bean2.getUid());
                break;
            case R.id.ln_3:
                intent.putExtra(BaseConfig.URL, BaseConfig.BaseWebURL + "/user/"+bean3.getUid());
                break;
        }
        if (intent != null){
            startActivity(intent);
        }

    }
}
